//
//  DeviceManager.m
//  UDKLib
//
//  Created by biao on 15/11/24.
//  Copyright © 2015年 biao.rao. All rights reserved.
//

#import "DeviceManager.h"

@implementation DeviceManager
-(DeviceType)getDeviceType:(NSDictionary *)advertisementData
{
    int type = 0;
//    NSDictionary *dic = (__bridge NSDictionary *)advertisementData;
//    NSLog(@"%@",dic);
    NSData *data = [advertisementData objectForKey:@"kCBAdvDataManufacturerData"];
    if (data == nil) {
        return UnKnownDevice;
    }
    const char *datebyte = (char *)data.bytes;
    if (datebyte != NULL) {
        type = [self getType:datebyte];
    }
    return type;
}
-(int)getType:(const char *)data
{
    int currentPos = 2;
    int mDeviceTypeCode = 0;
    NSLog(@"%lu",strlen(data));
    
    while (currentPos < strlen(data)) {
        int length = data[currentPos++] & 0xFF;
        if (length == 0) {
            break;
        }
        // Note the length includes the length of the field type itself.
        int dataLength = length - 1;
        int fieldType = data[currentPos++] & 0xFF;
        switch (fieldType) {
            case 0x02:
                //this data is used for IOS
                //in android we can get the mac address direct
                break;
            case 0x03:
                //高位在右
                mDeviceTypeCode =
                ((data[currentPos+1] & 0xFF) << 8) + (data[currentPos] & 0xFF);
                break;
        }
        currentPos += dataLength;
    }
    return mDeviceTypeCode;
    
}

@end
